package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsGateway;
import com.caiyi.financial.nirvana.sms.querybean.QueryGatewayBean;
import com.caiyi.financial.nirvana.sms.vo.SmsGatewayVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

@Mapper
public interface SmsGatewayMapper extends BaseMapper<SmsGateway> {

    @Select("select id,name from sms_gateway where type = #{operator} or type = 0")
    List<SmsGateway> queryByOperator(Integer operator);

    @Update("update sms_gateway set status=( case status when 1 then 2 else 1 end) where id=#{id}")
    int changeStatus(@Param("id") Integer id);

    @Update("update sms_gateway set status=#{status} where id=#{id}")
    int updateStatus(@Param("id") Integer id, @Param("status") Integer status);

    @Select("select id,name from sms_gateway")
    List<Map> getGatewayId();

    @Select("select * from sms_gateway where id=#{id}")
    SmsGateway getGateway(long id);

    @SelectProvider(type = GatewaySqlProvider.class, method = "selectGatewayListByParams")
    List<SmsGatewayVo> selectGatewayListByParams(QueryGatewayBean bean);

    class GatewaySqlProvider {
        public String selectGatewayListByParams(QueryGatewayBean bean) {
            return new SQL() {
                {
                    SELECT("id,name,code,type,province_code,protocol,server_addr,server_port,access_number,account,password,flow_speed,custom,status,alarm_line as alarmLine,alarm_phone as alarmPhone,create_time,update_time,notify_result,gateway_type,price,charging_type ");
                    FROM("sms_gateway");
                    if (!StringUtils.isEmpty(bean.getName())) {
                        WHERE("name LIKE concat('%',#{name},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getCode())) {
                        WHERE("code LIKE concat('%',#{code},'%')");
                    }
                    if (bean.getType() != null) {
                        WHERE("type=#{type}");
                    }
                    if (bean.getGatewayType() != null) {
                        WHERE("gateway_type=#{gatewayType}");
                    }
                    if (bean.getProtocol() != null) {
                        WHERE("protocol=#{protocol}");
                    }
                    if (!StringUtils.isEmpty(bean.getAccessNumber())) {
                        WHERE("access_number=#{accessNumber}");
                    }
                    if (bean.getStatus() != null) {
                        WHERE("status=#{status}");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");

                }
            }.toString();
        }

    }
}
